/*
 * @Author: your name
 * @Date: 2022-03-21 16:05:15
 * @LastEditTime: 2022-08-17 10:18:55
 * @LastEditors: YNT 2501512800@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \22-03-19 智慧社区\pages\index\index.js
 */
// pages/index/index.js
import { comm } from '../../behaviors/index'
import _ from '../../utils/lodash/index'
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog'
const computedBehavior = require('miniprogram-computed').behavior
Page({
  behaviors: [comm, computedBehavior],
  data: {
    nvabarData: {
      navigationBarTextStyle: 'white', // 胶囊主题 white || black
      navigationBarBackground: '#2ba6e2', // 导航栏背景色
      navigationBarTitleText: '',
      hideCapsule: true,
    },
    activeIndex: null, // 初始化为 null，不渲染任何tabber组件
    tabbars: [
      {
        name: '首页',
        normal: '/assets/images/tabbar/home.png',
        active: '/assets/images/tabbar/home_cur.png',
        show: false, // 控制组件是否被渲染
        status: false, // 控制组件的display
      },
      {
        name: '活动',
        normal: '/assets/images/tabbar/life.png',
        active: '/assets/images/tabbar/life_cur.png',
        show: false,
        status: false,
      },
      {
        name: '邻里',
        normal: '/assets/images/tabbar/neighbor.png',
        active: '/assets/images/tabbar/neighbor_cur.png',
        info: '',
        show: false,
        status: false,
      },
      {
        name: '我的',
        normal: '/assets/images/tabbar/user.png',
        active: '/assets/images/tabbar/user_cur.png',
        show: false,
        status: false,
      },
    ],
    score: 5,
    sign: false,
  },

  onLoad: function () {
    this.chooseTabbar()
  },

  // 选择tabbar后 》更改tabbar状态
  chooseTabbar(e = {}) {
    let { activeIndex: prevIndex } = this.data
    prevIndex = prevIndex || 0
    let currentIndex = _.get(e, 'detail', prevIndex)
    // 若点击的为当前选中项，则不执行。e.type 》区分用户点击触发、代码主动触发
    if (currentIndex === prevIndex && e.type) {
      return
    } else {
      // 修改上一个tab页面的status
      let prev = `tabbars[${prevIndex}].status`,
        // 修改当前选中元素的status
        status = `tabbars[${currentIndex}].status`,
        // 修改当前选中元素的show
        show = `tabbars[${currentIndex}].show`

      this.setData({
        [prev]: false,
        [status]: true,
        [show]: true,
        activeIndex: currentIndex,
      })
    }
  },

  // 提示
  tip: function (e) {
    let status = e.detail
    if (status == 1) {
      Toast('等待管理员完成实名审核')
    } else {
      Dialog.confirm({
        title: '提示',
        message: '请先完善实名信息',
        confirmButtonText: '页面跳转',
      })
        .then(() => {
          wx.navigateTo({
            url: '/subPackages/otherPackages/pages/realnameregistration/index',
          })
        })
        .catch(() => {
          // on cancel
        })
    }
  },

  hasSigned: function () {
    Toast('今日已完成签到~')
  },
  popup: function (e) {
    this.setData({
      score: e.detail,
      sign: !this.data.sign,
    })
  },

  onShareAppMessage: function (res) {
    var shareObj = {
      title: '和你一起创未来', // 默认是小程序的名称(可以写slogan等)
      path: '/pages/loading/index', // 默认是当前页面，必须是以‘/'开头的完整路径
      imageUrl: '/assets/images/logo.png', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {},
      fail: function () {},
      complete: function () {},
    }
    return shareObj
  },
})
